<template>
  <div class="gray-page resident-detail-page">
    <nav-bar :title="title"></nav-bar>
    <div class="page-main">
      <van-sticky class="page-scroll" :offset-top="44">
        <van-form
          ref="from"
          :show-error="false"
          input-align="right"
          error-message-align="right"
          validate-trigger="onSubmit"
          :label-width="120"
          @submit="onSubmit"
          @failed="onFailed"
        >
          <van-field
            required
            :rules="rules.disposalResults"
            v-model="formParams.disposalResults"
            rows="4"
            autosize
            label="完成记录"
            type="textarea"
            maxlength="500"
            placeholder="请输入完成记录详情描述......"
            show-word-limit
          />
          <van-field label="添加图片/视频" readonly :border="false">
            <template #input>
              <van-radio-group v-model="accept" direction="horizontal">
                <van-radio name="image/*">图片</van-radio>
                <van-radio name="video/*">视频</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field class="van-field__value--upload" input-align="left">
            <template #input>
              <van-uploader
                name="fileList"
                v-model="fileList"
                max-count="9"
                :accept="accept"
                :max-size="maxSize"
                :preview-image="previewImage"
                :before-read="uploadBeforeRead"
                :after-read="uploadAfterRead"
                @click-preview="previewClick"
              >
                <template #preview-cover="file">
                  <div v-if="format.includes(file.file.name.split('.')[file.file.name.split('.').length - 1])">
                    <div class="van-uploader__preview-masker"></div>
                    <video-player
                      class="video-player vjs-custom-skin"
                      :playsinline="true"
                      :options="{
                        aspectRatio: '16:16',
                        sources: [
                          {
                            src: file.file.url
                          }
                        ]
                      }"
                    ></video-player>
                  </div>
                </template>
                <div class="van-uploader__input--white">
                  <div class="van-uploader__input--content">
                    <div class="upload-image">
                      <img src="@/assets/images/basic/icon_camera@2x.png" />
                    </div>
                    <div class="upload-text">点击拍摄</div>
                  </div>
                </div>
              </van-uploader>
            </template>
          </van-field>
          <div style="margin: 32px 16px">
            <van-row :gutter="23">
              <van-col span="12">
                <van-button block native-type="button" @click="onClear">取消</van-button>
              </van-col>
              <van-col span="12">
                <van-button block type="info" native-type="submit" :disabled="buttonDisabled">提交</van-button>
              </van-col>
            </van-row>
          </div>
        </van-form>
      </van-sticky>
    </div>
    <!-- 视频预览 -->
    <gb-video :url="videoUrl" :show.sync="videoPopupShow"></gb-video>
  </div>
</template>

<script>
import Upload from '@/mixins/upload'
import Routine from '@/api/routine'
export default {
  mixins: [Upload],
  data() {
    return {
      title: '领导任务',
      buttonDisabled: false,
      folder: 'task',
      accept: 'image/*',
      maxSize: 50 * 1024 * 1024,
      format: ['mp4', 'avi', 'mov'],
      fileList: [],
      videoUrl: '',
      videoPopupShow: false,
      disposalPicture: [],
      formParams: {
        disposalResults: '',
        disposalPicture: ''
      },
      rules: {
        disposalResults: [{ required: true, message: '请填写完成记录' }]
      }
    }
  },
  methods: {
    // 视频预览
    previewClick({ file }, detail) {
      const urlArr = this.fileList[detail.index].url.split('.')
      const suffix = urlArr[urlArr.length - 1]
      if (this.format.includes(suffix)) {
        this.videoPopupShow = true
        this.videoUrl = this.$config.ImageURl + this.fileList[detail.index].url
      }
    },
    onSubmit() {
      this.buttonDisabled = true
      this.formParams.button = this.$route.query.button
      this.formParams.taskCode = this.$route.query.taskCode
      this.formParams.disposalPicture = this.fileList.map(o => o.url).join(',')
      Routine.Task.update(this.formParams)
        .then(res => {
          const { status } = res.data
          if (status === 0) {
            this.$router.push({
              path: '/page/message',
              query: {
                backTo: '-3',
                title: '任务已完成'
              }
            })
          } else {
            this.$toast.fail(res.data.message)
          }
          this.buttonDisabled = false
        })
        .catch(() => {
          this.buttonDisabled = false
        })
    },
    onClear() {
      this.$dialog
        .confirm({
          title: '提示',
          message: '是否清空所有内容',
          confirmButtonText: '是',
          cancelButtonText: '否'
        })
        .then(() => {
          this.formParams.disposalResults = ''
          this.formParams.disposalPicture = ''
        })
    },
    // 校验失败
    onFailed(errorInfo) {
      this.$refs.from.scrollToField(errorInfo.errors[0].name)
    }
  }
}
</script>

<style></style>
